home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4074 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.2 KB

  1. Path: news.Stanford.EDU!usenet
  2. From: brien@leland.stanford.edu (brien oberstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Copy constructing an already default constructed object
  5. Date: Sat, 27 Jan 1996 18:47:07 GMT
  6. Organization: Stanford University
  7. Message-ID: <310a717f.5074136@nntp>
  8. References: <4e906b$stk@elaine32.Stanford.EDU> <4eal0n$hgq@dawn.mmm.com> <3108ef14.340699@nntp> <4ebmst$7cl@dawn.mmm.com>
  9. NNTP-Posting-Host: tip-mp9-ncs-15.stanford.edu
  10.  
  11. On 26 Jan 1996 23:08:13 GMT, kjhopps@mmm.com (Kevin J Hopps) wrote:
  12.  
  13. >Personally, I find the assignment operator almost exactly as
  14. >"painful" to write as the copy constructor.  But if for some
  15. >reason it is more painful to initialize objects in operator=
  16. >than in the copy constructor, you could write operator= this way:
  17. >    void A::operator=(const A& rhs)
  18. >    {
  19. >    if (this != &rhs) {
  20. >        this->~A();
  21. >        new (this) A(rhs);
  22. >    }
  23. >    }
  24. >
  25. >But for two members, it's the same number of lines anyway :-)
  26.  
  27. Beautiful!  This is exactly what I was looking for.  Is this
  28. completely safe? Are there any situations I to watch out for when
  29. using this construct?  I've never used templates, but I think that
  30. they would be perfect for this.
  31.  
  32. -brien
  33.